package com.hongxun.budget.ui.activity.tk;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.dialog.PullLineDialog;
import com.hongxun.budget.utils.DrawMapUtils;
import com.hongxun.greenentity.tk.DDrawLineEntity;
import com.hongxun.interfaces.ResponseListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PullLineActivity extends BaseActivity {
    @BindView(R.id.map)
    MapView mMapView;
    AMap aMap;
    Double lat, lgt;

    @BindView(R.id.right_save)
    Button rightSave;
    List<DDrawLineEntity> lineEntities = new ArrayList<>();
    List<DDrawLineEntity> deleteEntities = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_pull_line;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        rightSave.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        lat = intent.getDoubleExtra("lat", 0);
        lgt = intent.getDoubleExtra("lgt", 0);
        lineEntities.addAll((Collection<? extends DDrawLineEntity>) intent.getSerializableExtra("insert"));
        for (DDrawLineEntity d : lineEntities) {
            if (d.getDistance() == null) {
                Double dis=Double.valueOf(AMapUtils.calculateLineDistance(new LatLng(lat, lgt), new LatLng(d.getDLLatitude(), d.getDLLongitude())));
                d.setDistance((double) Math.round(dis * 100) / 100);
            }
        }
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
            @Override
            public void onMapClick(final LatLng latLng) {
                new PullLineDialog().setListener(new ResponseListener() {
                    @Override
                    public void response(Object... data) {
                        DDrawLineEntity d = (DDrawLineEntity) data[0];
                        float v = AMapUtils.calculateLineDistance(latLng, new LatLng(lat, lgt));
                        double x = (latLng.latitude - lat) / v;
                        double y = (latLng.longitude - lgt) / v;
                        d.setDLLatitude(lat + x * d.getDistance());
                        d.setDLLongitude(lgt + y * d.getDistance());
                        lineEntities.add(d);
                        setLine();
                    }
                }).show(getFragmentManager(), "");
            }
        });

        setLine();
        aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(final Marker marker) {
                if (marker.getObject() instanceof DDrawLineEntity)
                    new PullLineDialog().setListener(new ResponseListener() {
                        @Override
                        public void response(Object... data) {
                            if (data[0] instanceof String) {
                                lineEntities.remove((DDrawLineEntity) marker.getObject());
                                deleteEntities.add((DDrawLineEntity) marker.getObject());
                            } else if (data[0] instanceof DDrawLineEntity) {
                                lineEntities.remove((DDrawLineEntity) marker.getObject());
                                lineEntities.add((DDrawLineEntity) data[0]);
                            }
                            setLine();
                        }
                    }).setEntity((DDrawLineEntity) marker.getObject()).show(getFragmentManager(), "");
                return true;
            }
        });
    }

    void setLine() {
        DrawMapUtils utils = new DrawMapUtils(this, aMap, false, false, "显示点位");
        utils.setWork("全部");
        utils.draw();
        setLocation(new LatLng(lat, lgt));
        for (DDrawLineEntity d : lineEntities) {
            List<LatLng> latLngs = new ArrayList<LatLng>();
            latLngs.add(new LatLng(lat, lgt));
            latLngs.add(new LatLng(d.getDLLatitude(), d.getDLLongitude()));
            aMap.addPolyline(new PolylineOptions().
                    addAll(latLngs).width(10).color(Color.argb(255, 1, 1, 1)));
            Marker lxMarker = aMap.addMarker(new MarkerOptions().position(new LatLng(d.getDLLatitude(), d.getDLLongitude()))
                    .anchor(0.5f, 0.5f)
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.p_laxian_show)));
            lxMarker.setObject(d);
        }

    }

    void setLocation(LatLng location) {
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, 20);
        aMap.animateCamera(cameraUpdate);
        aMap.addMarker(new MarkerOptions().position(location)
                .anchor(0.5f, 0.5f)
                .title("杆塔")
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.purple_pin)));
    }

    @OnClick(R.id.right_save)
    public void onViewClicked() {
        Intent intent = new Intent();
        intent.putExtra("insert", (Serializable) lineEntities);
        intent.putExtra("delete", (Serializable) deleteEntities);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}
